package com.yushu.authority.controller;

import com.yushu.authority.pojo.bo.PmMenu;
import com.yushu.authority.pojo.bo.PmRole;
import com.yushu.authority.pojo.vo.PmRoleVO;
import com.yushu.authority.pojo.vo.PmUserVO;
import com.yushu.authority.service.PmRoleService;
import com.yushu.common.anno.LogAop;
import com.yushu.common.execption.CgException;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


 /**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
//@Api(description = "角色管理")
@Controller
@RequestMapping("/authority/pmRole")
public class PmRoleController {
   private Logger logger = Logger.getLogger(PmRoleController.class);

    @Autowired
    private PmRoleService pmRoleService;
    private Map<String, Object> resultMap;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    /**
     * 分页条件查询角色数据
     * 2019-07-24 19:49:55
     * <p>
     * 2020-04-02 15:47:08
     *
     * @param tablePage
     * @return
     */
    @LogAop("分页查询角色")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Pages page(@RequestBody Pages<PmRoleVO> tablePage) {
        Pages page = pmRoleService.page(tablePage);
        /*System.out.println(tablePage.getData().getOrgId());*/
        return page;
    }


    /**
     * 查询全部角色数据
     * 2019-07-24 19:49:55
     * <p>
     * 2020-04-02 15:47:08
     */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<PmRole> queryList(@RequestBody PmRole pmRole) {
        List<PmRole> list = pmRoleService.selectList(pmRole);
        return list;
    }

    /**
     * 查询全部角色数据
     * 2019-07-24 19:49:55
     * <p>
     * 2020-04-02 15:47:08
     */
    @ResponseBody
    @RequestMapping(value = "/queryProjectRoles", method = RequestMethod.GET)
    public List<PmRole> queryProjectRoles() {
        List<PmRole> list = pmRoleService.queryProjectRoles();
        return list;
    }


    /**
     * 添加或者更新角色
     * 2019-07-24 19:49:55
     * <p>
     * 2020-04-02 15:47:08
     *
     * @param pmRole
     * @return
     */
    @LogAop("新增角色")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody PmRole pmRole) {
        try {
            logger.info("进入添加角色方法");
            pmRoleService.insert(pmRole);
            return Result.success("角色新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

     //编辑角色
     @LogAop("编辑角色")
     @RequestMapping(value = "/edit", method = RequestMethod.POST)
     @ResponseBody
     public Result edit(@RequestBody PmRole pmRole) {
         try {
             logger.info("进入修改角色方法");
             pmRoleService.updateByKey(pmRole);
             return Result.success("角色修改成功");
         } catch (Exception e) {
             e.printStackTrace();
             ;
             return Result.success(e.getMessage());
         }
     }

     /*校验角色名称是否重复*/
     @ResponseBody
     @RequestMapping(value = "/validateRoleName", method = RequestMethod.POST)
     public String validateRoleName(String roleName, String key) {
         try {
             return pmRoleService.validateRoleName(roleName, key);
         } catch (Exception e) {
             e.printStackTrace();
             return "false";
         }
     }



    /**
     * 删除角色数据
     * 2020-04-02 15:47:08
     * 2019-07-24 19:49:54
     *
     * @param pmRole 需要删除的对象
     */
    @LogAop("删除角色")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(Model model, @RequestBody PmRole pmRole) {
        try {
            pmRoleService.deleteById(pmRole.getId());
            logger.info("删除角色成功");
            return Result.success("删除角色成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 批量删除角色数据
     * 2019-07-24 19:49:54
     * TODO 此处可以借鉴
     * 2020-04-02 15:47:08
     *
     * @param pmRoles 角色集合
     */
    @LogAop("批量删除角色")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public Result batchDelete(@RequestBody List<PmRole> pmRoles) {
        logger.info("进入到删除角色数据方法");
        try {
            pmRoleService.deleteBatch(pmRoles);
            return Result.success("批量删除角色成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器异常");

        }
    }


    /**
     * 跳转角色列表页面
     * 2020-04-02 15:47:08
     */
    @RequestMapping(value = "toPmRoleList", method = RequestMethod.GET)
    public String toPmRoleList(Model model) {
        model.addAttribute("title", "角色管理");
        model.addAttribute("page", "authority/pmRole/list");
        return "menuPage";
    }


//    /**
//     * 跳转角色跳转到空白列表页面
//     * 2019-07-24 19:49:55
//     * <p>
//     * 2020-04-02 15:47:08
//     */
//    @RequestMapping(value = "toOpenPmRoleList", method = RequestMethod.GET)
//    public String toOpenPmRoleList(Model model, HttpServletRequest request) {
//        model.addAttribute("title", "角色管理");
//        model.addAttribute("page", "authority/pmRole/list");
//        return "openPage";
//    }


//    /**
//     * 跳转角色新增或修改页面
//     * 2019-07-24 19:49:55
//     * 2020-01-01 10:59:03
//     * 2020-04-02 15:47:08
//     */
//    @RequestMapping(value = "toEditor", method = RequestMethod.GET)
//    public String toEditor(Model model, String id, HttpServletRequest request) {
//        if (StringUtils.isNotEmpty(id)) {
//            request.setAttribute("data", pmRoleService.selectById(id));
//        }
//        model.addAttribute("title", "角色管理");
//        model.addAttribute("page", "authority/pmRole/addUpdate");
//        return "openPage";
//    }


    @RequestMapping("checkRepeat")
    @ResponseBody
    public String checkRepeat(@RequestBody Map map) {
        return pmRoleService.checkRepeat(map);
    }


    /**
     * 查询已选择角色的菜单的交集
     * 2019-7-26 11:18:33
     */
    @LogAop("查询已选择角色的菜单的交集")
    @ResponseBody
    @RequestMapping(value = "/queryIntersections", method = RequestMethod.POST)
    public List<PmMenu> queryIntersections(@RequestBody String[] selectTableDatasJson) {
        List<PmMenu> list = pmRoleService.queryIntersections(selectTableDatasJson);
        return list;
    }

    /**
     * 询当前所选择用户下的角色的交集
     * 2019-7-26 11:18:33
     */
    @ResponseBody
    @RequestMapping(value = "/queryRolesOnUsersIntersections", method = RequestMethod.POST)
    public List<PmRole> queryRolesOnUsersIntersections(@RequestBody List<PmUserVO> users) {
        logger.info("询当前所选择用户下的角色的交集");
        List<PmRole> list = pmRoleService.queryRolesOnUsersIntersections(users);
        return list;
    }


    /**
     * <pre>查询当前用户下的角色
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/31 15:27
     */
    @ResponseBody
    @RequestMapping(value = "queryRolesOnUsers", method = RequestMethod.POST)
    public List<PmRole> queryRolesOnUsers(@RequestBody List<PmUserVO> users) {
        logger.info("查询当前用户下的角色");
        try {
            return pmRoleService.queryRolesOnUsers(users);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<PmRole>();
        }
    }


    /**
     * 更新用户角色关联信息
     * 2019-06-20 07:33:38
     *
     * @param
     * @return
     */
    @LogAop("更新用户角色关联信息")
    @RequestMapping(value = "updateRoleOnUser", method = RequestMethod.POST)
    @ResponseBody
    public Map updateRoleOnUser(Model model, @RequestBody Map roleUserData) {
        logger.info("更新用户角色关联信息方法");
        try {
           /* System.out.println(roleUserData.toString());*/
            pmRoleService.updateRoleOnUser(roleUserData);
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
        }
        return new HashMap();
    }


//    /**
//     * 跳转到分配菜单权限页面
//     * 2019-7-25 18:15:05
//     */
//    @RequestMapping(value = "toAssignMenu", method = RequestMethod.GET)
//    public String toAssignMenu(Model model, HttpServletRequest request) {
//        model.addAttribute("title", "分配菜单权限");
//        model.addAttribute("page", "/authority/pmRole/assignMenu");
//        return "openPage";
//    }
//
//    /**
//     * 跳转到分配搜索框权限页面
//     * 2019-8-5 09:33:35
//     */
//    @RequestMapping(value = "toAssignSearchBox", method = RequestMethod.GET)
//    public String toAssignSearchBox(Model model, HttpServletRequest request) {
//        model.addAttribute("title", "分配搜索框权限");
//        model.addAttribute("page", "/authority/pmRole/assignSearchBox");
//        return "openPage";
//    }
//
//
//    /**
//     * 跳转到分配列表数据列权限页面
//     * 2019-8-6 09:33:28
//     */
//    @RequestMapping(value = "toAssignButton", method = RequestMethod.GET)
//    public String toAssignButton(Model model, HttpServletRequest request) {
//        model.addAttribute("title", "分配按钮权限");
//        model.addAttribute("page", "/authority/pmRole/assignMenu");
//        return "openPage";
//    }
//

    /**
     * 更新角色菜单关联表
     * 2019-7-25 21:20:14
     *
     * @param
     * @return
     */
    @RequestMapping(value = "updateMenuOnRole", method = RequestMethod.POST)
    @ResponseBody
    public Map updateMenuOnRole(Model model, @RequestBody Map ruleMenuData) {
        logger.info("更新菜单角色关联表");
        try {
            pmRoleService.updateMenuOnRole(ruleMenuData);
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
        }
        return new HashMap();
    }

    /**
     * 更新角色搜索框权限关联表
     * 2019-8-7 10:08:33
     *
     * @param
     * @return
     */
    @RequestMapping(value = "updateSearchBoxOnRole", method = RequestMethod.POST)
    @ResponseBody
    public Map updateSearchBoxOnRole(@RequestBody Map boxRoleData) {
        logger.info("更新角色搜索框权限关联表");
        try {
            pmRoleService.updateSearchBoxOnRole(boxRoleData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();
    }
}

